package com.listener.controller;

import com.listener.dao.AbilityDao;
import com.listener.dao.MoveDao;
import com.listener.dao.PokemonDao;
import com.listener.pojo.Ability;
import com.listener.pojo.Move;
import com.listener.pojo.Page;
import com.listener.pojo.Pokemon;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@Controller
public class TestController {
    @Autowired
    AbilityDao abilityDao;
    @Autowired
    MoveDao moveDao;
    @Autowired
    PokemonDao pokemonDao;
    @RequestMapping("/selectAbilityTest")
    public void selectAbilityTest(Integer pageNum, HttpServletResponse response) throws IOException {
        if(pageNum==null){pageNum=1;}
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        List<Ability> abilities =
                abilityDao.selectAbilityPage(new Ability(),
                        pageNum,10).getPage();
        for (Ability ability : abilities) {
            out.println("id:【"+ability.getAbilityId()+"】,特性名:【<a href="+ability.getWikiUrl()+">" +
                    ability.getAbility()+ "</a>】,描述:【"+
                    ability.getDescription()+"】"+"<br>");
        }
    }
    @RequestMapping("/selectMoveTest")
    public void selectMoveTest(Integer pageNum, HttpServletResponse response) throws IOException {
        if(pageNum==null){pageNum=1;}
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        List<Move> moves =
                moveDao.selectMovePage(new Move(),
                        pageNum,10).getPage();
        for (Move move : moves) {
            out.println("id:【"+move.getMoveId()+"】,招式名:【<a href="+move.getWikiUrl()+">" +
                    move.getMove()+ "</a>】,描述:【"+
                    move.getDescription()+"】"+"<br>");
        }
    }

    @RequestMapping("/selectPokemonTest")
    public void selectPokemonTest(Integer pageNum,HttpServletRequest request, HttpServletResponse response) throws IOException {
        if(pageNum==null){pageNum=1;}
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        List<Pokemon> pokemons =
                pokemonDao.selectPokemonPage(new Pokemon(),
                        pageNum,10).getPage();
        for (Pokemon pokemon : pokemons) {
            out.println("<img style=\"width:40px;height:40px\" src="+
                    request.getContextPath()+'/'+pokemon.getPicture()+" />"+"id:【"+pokemon.getPokemonId()+"】,名称:【<a href="+pokemon.getWikiUrl()+">" +
                    pokemon.getPokemon()+ "</a>】<br>");
        }
    }
    @RequestMapping("/selectMoveByPokemon")
    public void selectMoveByPokemon(String pokemon,HttpServletResponse response) throws IOException {
        if(pokemon == null){
            pokemon="沙奈朵";
        }
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        List<Map<String, Object>> maps = pokemonDao.selectMoveByPokemon(pokemon);
        for (Map<String, Object> map : maps) {
            out.println("招式名:【"+map.get("move")+"】,学会等级:【"+map.get("level")+"】<br>");
        }
    }
}
